package com.easyattendance.servlets;


import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.easyattendance.model.User;
import com.easyattendance.services.LoginService;
import com.easyattendance.services.ValidationsService;
import com.easyattendance.util.SecurePassword;


@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    /**
	 * 
	 */
	private static final long serialVersionUID = -7436224964012364053L;

	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {        
        
		String emailId = request.getParameter("email");        
        String password = request.getParameter("password");
        String link=request.getParameter("forgot_link");
        
        LoginService loginService = new LoginService();
      
      if(emailId!=null && password!=null)
      {
        boolean result = loginService.authenticate(emailId, password);
        String userType = loginService.getUserType(emailId);
        
        boolean enabled=loginService.checkIsEnabled(emailId);
        if(result == true && enabled){
            request.getSession().setAttribute("currentUser", emailId);
            
            boolean firstTimeLogin=loginService.checkIfLoggedInForFirstTime(emailId);
            
            if(userType.equals("Section") && !(firstTimeLogin)) {
            	response.sendRedirect("views/section/reports.jsp");
            }
            else if(userType.equals("Admin") || userType.equals("admin") && !(firstTimeLogin)) {
            	response.sendRedirect("views/admin/reports.jsp");
            }
            else if(userType.equals("Teacher") && !(firstTimeLogin)) {
            	response.sendRedirect("views/teacher/takeAttendance.jsp");
            }
            else if(firstTimeLogin && userType.equalsIgnoreCase("Admin")){
            	response.sendRedirect("views/admin/passChange.jsp");
            }
            else if(firstTimeLogin && userType.equals("Teacher")){
            	response.sendRedirect("views/teacher/generalSettings.jsp");
            }
            else if(firstTimeLogin && userType.equals("Section Employee")){
            	response.sendRedirect("views/section/generalSettings.jsp");
            }
            else if(userType.equals("Student"))
            {
            	response.sendRedirect("views/student/viewAssignment.jsp");
            }
            else {
            	response.sendRedirect("views/login.jsp");
            }
            
        }
        else{
        	if(!result){
	            request.setAttribute("loginFail", "Email and password not matched.Please try again.");
	        	RequestDispatcher rd = request.getRequestDispatcher("views/login.jsp");
	        	rd.forward(request, response);
        	}
        	else if(!enabled)
        	{
        		 request.setAttribute("notEnabled", "Please activate your account by clicking on link sent to your mail.");
 	           	 RequestDispatcher rd = request.getRequestDispatcher("views/login.jsp");
 	        	 rd.forward(request, response);
        		
        	}
        }
      }
      else if(link.equals("forgot_password"))
      {
    	    boolean emailId_exists=loginService.ifAlreadyExists(emailId);
    	    if(emailId_exists)
    	    {
    	    	String new_password=loginService.generatePassword();
    	    	User user=(User) loginService.getUserByUserId(emailId,"User");
    	    	String hash_Password = SecurePassword.getHashValue(new_password);
    	    	loginService.updatePassword(user, hash_Password);
    	    	loginService.SendMail(emailId, new_password);
    	    	
    	    	 request.setAttribute("emailSent","Check your mail for new password");
    	    	 RequestDispatcher rd = request.getRequestDispatcher("views/forgotPassword.jsp");
    	    	 rd.forward(request, response);
    	    }
    	    else
    	    {
    	    	 request.setAttribute("emailIdNotExists","Email Id does not exist");
    	    	 RequestDispatcher rd = request.getRequestDispatcher("views/forgotPassword.jsp");
    	    	 rd.forward(request, response);
    	    }
      }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
